home *** CD-ROM | disk | FTP | other *** search
/ MacPeople 2003 February 1 / MACPEOPLE-2003-02-01.ISO.7z / MACPEOPLE-2003-02-01.ISO / ぶらりオンラインウェアの旅 / おしゃべり漂流記 / xGates / xGates 1.2 Source Code.sit / xGates 1.2 Source Code / prefs.c < prev    next >
Text File  |  2002-12-08  |  5KB  |  203 lines

  1. /*
  2.     xGates -- Stunningly entertaining action game for MacOS Classic / MacOS X
  3.     Copyright (C) 2002 Sveinbjorn Thordarson <paladeen@soth.zoneit.com>
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.     prefs.c
  20.  
  21. */
  22.  
  23.  
  24. #include "externs.h"
  25. #include "prototypes.h"
  26. #include "definitions.h"
  27.  
  28.  
  29. ////////////////////////////////////////////////
  30. //Locate Preferences file and load it, regardless
  31. ////////////////////////////////////////////////
  32.  
  33. void LoadPrefs (void)
  34. {
  35.     OSErr     myErr = noErr;
  36.     FSSpec     file;
  37.     short     refNum;
  38.     short     prefRefNum;
  39.     long     prefDirID;
  40.     
  41.     //locate preferences folder
  42.     myErr = FindFolder(kOnSystemDisk, kPreferencesFolderType, false, &prefRefNum, &prefDirID);
  43.     if (myErr)
  44.     {
  45.         DoAlert("¥pError reading preferences.", "¥pCould not locate Preferences folder.");
  46.         QuitApp();
  47.     }
  48.     
  49.     //create the file pref spec to open
  50.     FSMakeFSSpec(prefRefNum, prefDirID, kPreferencesFileName, &file);
  51.     
  52.     //try to open it
  53.     myErr = FSpOpenDF(&file, fsRdPerm, &refNum);
  54.     FSClose(refNum);
  55.     
  56.     //if open fails, create the default prefs file
  57.     if (myErr)
  58.     {
  59.         CreatePrefs(file);
  60.     }
  61.     //otherwise...read 'em
  62.     else
  63.     {
  64.         //load spec into the prefs global structure
  65.         prefsFile = file;
  66.         //read
  67.         ReadPrefs(prefsFile);
  68.     }
  69. }
  70.  
  71.  
  72. ////////////////////////////////////////////////
  73. //Read prefs file into prefsStruct structure
  74. ////////////////////////////////////////////////
  75.  
  76.  
  77. void ReadPrefs (FSSpec fileSpec)
  78. {
  79.         OSErr myErr = noErr;
  80.         long count;
  81.         short refNum;
  82.         
  83.         myErr = FSpOpenDF(&fileSpec, fsRdPerm, &refNum);
  84.         
  85.         if (myErr)
  86.         {
  87.             DoAlert("¥pError opening preferences.", "¥pTry deleting the 'xGates Preferences' file in Preferences folder.");
  88.             QuitApp();
  89.         
  90.         }
  91.         count = sizeof(short);
  92.         FSRead(refNum, &count,  &prefs.appVersionMajor);
  93.         FSRead(refNum, &count,  &prefs.appVersionMinor);
  94.         
  95.         //if it's a prefs file for an older/newer version 
  96.         if (    prefs.appVersionMajor != kAppVersionMajor ||
  97.                 prefs.appVersionMinor != kAppVersionMinor )
  98.         {
  99.             FSClose(refNum);
  100.             CreatePrefs(fileSpec);
  101.             return;
  102.         }
  103.         
  104.         //read the data from file into fields in prefs structure
  105.         count = sizeof(HighScoreStruct);
  106.         FSRead(refNum, &count,  &prefs.highscores);
  107.         count = sizeof(short);
  108.         FSRead(refNum, &count, &prefs.music);
  109.         FSRead(refNum, &count, &prefs.sound);
  110.         FSRead(refNum, &count, &prefs.lastHighScore);
  111.         FSRead(refNum, &count, &prefs.noChainsawJamming);
  112.         FSRead(refNum, &count, &prefs.fixedFrameRate);
  113.         FSRead(refNum, &count, &prefs.drawFPS);
  114.  
  115.         FSClose(refNum);
  116. }
  117.  
  118.  
  119. ////////////////////////////////////////////////
  120. //Generate new prefs file with defaults
  121. ////////////////////////////////////////////////
  122.  
  123. void CreatePrefs (FSSpec fileSpec)
  124. {
  125.     OSErr   myErr = noErr;
  126.     
  127.     //set the defaults
  128.     prefs.appVersionMajor = kAppVersionMajor;
  129.     prefs.appVersionMinor = kAppVersionMinor;
  130.     
  131.     ClearHighscoreList();//equivalent to creating a new one
  132.  
  133.  
  134.     prefs.music = true;
  135.     prefs.sound = true;
  136.     prefs.lastHighScore = kNoHighScoreSet;
  137.     prefs.noChainsawJamming = false;
  138.     prefs.fixedFrameRate = true;
  139.     prefs.drawFPS = false;
  140.     
  141.     
  142.     //copy file spec into global prefs structure
  143.     prefsFile = fileSpec;
  144.     
  145.     //create it on disk
  146.     myErr = FSpCreate(&prefsFile, 'xGat', 'pref', smSystemScript);
  147.     
  148.     if (myErr == -48) //file already exists, then delete it and try again
  149.     {
  150.         HDelete(prefsFile.vRefNum, prefsFile.parID, prefsFile.name);
  151.         myErr = FSpCreate(&prefsFile, 'xGat', 'pref', smSystemScript);
  152.     }
  153.     //then write the defaults to it
  154.     if(myErr || WritePrefs())
  155.     {
  156.         DoAlert("¥pError creating/writing preferences.", "¥pTry deleting the 'xGates Preferences' file in the Preferences folder.");
  157.         QuitApp();
  158.     }
  159.  
  160. }
  161.  
  162. ////////////////////////////////////////////////
  163. //Write prefsStruct structure to file
  164. ////////////////////////////////////////////////
  165.  
  166. short WritePrefs (void)
  167. {
  168.     OSErr   myErr = noErr;
  169.     short   refNum;
  170.     long    count;
  171.     
  172.     //open with file spec from prefs
  173.     myErr = FSpOpenDF(&prefsFile, fsRdWrPerm, &refNum);
  174.     
  175.     if (myErr)
  176.     {
  177.         DoAlert("¥pError writing preferences.", "¥pTry deleting the 'xGates Preferences' file in Preferences folder.");
  178.         QuitApp();
  179.     }
  180.     
  181.     //write the prefs
  182.     count = sizeof(short);
  183.     FSWrite(refNum, &count, &prefs.appVersionMajor);
  184.     FSWrite(refNum, &count, &prefs.appVersionMinor);
  185.     
  186.     count = sizeof(HighScoreStruct); 
  187.     FSWrite(refNum, &count, &prefs.highscores);
  188.     
  189.     count = sizeof(short);
  190.  
  191.     FSWrite(refNum, &count, &prefs.music);
  192.     FSWrite(refNum, &count, &prefs.sound);
  193.     FSWrite(refNum, &count, &prefs.lastHighScore);
  194.     FSWrite(refNum, &count, &prefs.noChainsawJamming);
  195.     FSWrite(refNum, &count, &prefs.fixedFrameRate);
  196.     FSWrite(refNum, &count, &prefs.drawFPS);
  197.     
  198.     FSClose(refNum);
  199.     
  200.     return(myErr);
  201. }
  202.  
  203.